package com.bootdo.bj.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.Tree;
import com.alibaba.druid.util.StringUtils;
import com.bootdo.bj.domain.Category;
import com.bootdo.bj.service.CategoryService;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;


/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-01-23 17:22:27
 */
 
@Controller
@RequestMapping("/bj/category")
public class CategoryController extends BaseController{


	@Autowired
	private CategoryService categoryService;
	
	@ModelAttribute
	public Category get(@RequestParam(required=false) Integer id) {
		if (id != null){
			return categoryService.get(id);
		}else{
			return new Category();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/bj/category','bj:category:category',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','bj:category:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("bj:category:category")	
	String Category(Category category,HttpServletRequest request,HttpServletResponse response){
	    return "bj/category/category";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("bj:category:category")
	public List<Category>   list(@RequestParam Map<String, Object> params){
	 
		params.put("userId", getUserId());
	 
		//查询列表数据
        Query query = new Query(params);
		List<Category> categoryList = categoryService.list(query);
		/*int total = categoryService.count(query);
		PageUtils pageUtils = new PageUtils(categoryList, total);*/
		return categoryList;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("bj:category:edit")
	String add(Category category,HttpServletRequest request,HttpServletResponse response){
	    return "bj/category/categoryedit";
	}
	
	
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("bj:category:edit")
	public R save( Category category){
		category.setUserId(getUserId());
		category.setUserName(getUsername());
		if(categoryService.save(category)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("bj:category:edit")
	public R remove( Integer id){
		if(categoryService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("bj:category:edit")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		categoryService.batchRemove(ids);
		return R.ok();
	}
	
	
	/**
	 * 树
	 * http://127.0.0.1/hjb/goodCategory/tree
	 */
	@RequestMapping("/tree")
	@ResponseBody
	@RequiresPermissions("bj:category:edit")
	public Tree<Category> tree(HttpServletRequest request){
		Integer parentId =  StringUtils.isEmpty(request.getParameter("parentId")) ? null:Integer.parseInt(request.getParameter("parentId"));
		if(null == parentId) {
			return getTree();
		}else {
			return getTree(parentId);
		}
		
	}
	
	public Tree<Category> getTree() {
		List<Tree<Category>> trees = new ArrayList<Tree<Category>>();
		 HashMap<String,Object> params  = new HashMap<String,Object>(16);
		params.put("userId", getUserId());	
		List<Category> sysDepts = categoryService.list(params);
		for (Category sysDept : sysDepts) {
			Tree<Category> tree = new Tree<Category>();
			tree.setId(sysDept.getId().toString());
			tree.setParentId(sysDept.getParentId().toString());
			tree.setText(sysDept.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<Category> t = BuildTree.build(trees);
		return t;
	}
	
	/**
	 * 如果传入的id 和  树里的数据相等 就选中
	 * @param id
	 * @return
	 */
	public Tree<Category> getTree(Integer id) {
		
		// 根据roleId查询权限
		List<Tree<Category>> trees = new ArrayList<Tree<Category>>();
		List<Category> menuDOs = categoryService.list(new HashMap<String, Object>(16));
		for (Category sysMenuDO : menuDOs) {		
				Tree<Category> tree = new Tree<Category>();
				tree.setId(sysMenuDO.getId().toString());
				tree.setParentId(sysMenuDO.getParentId().toString());
				tree.setText(sysMenuDO.getName());
				Map<String, Object> state = new HashMap<>(16);
				Integer menuId = sysMenuDO.getId();
				if (menuId.equals(id)) {				
					state.put("selected", true);				
				} else {
					state.put("selected", false);
				}
				tree.setState(state);	
				trees.add(tree);
				
		}
		
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<Category> t = BuildTree.build(trees);
		
		// 小bug
		if(id.equals(-1)) {
			Map<String, Object> state = new HashMap<>(16);
			state.put("selected", true);
			t.setState(state);
		}
		
		return t;
	}
	
}